package com.wxy.demo;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.wxy.demo.constant.PublicConstants;
import com.wxy.demo.dto.BodyParam;
import com.wxy.demo.dto.DataDto;
import com.wxy.demo.dto.UserDto;
import com.wxy.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/5/31 22:23
 */
@Slf4j
public class JsonTest1 {

    /**
     * json字符串转对象
     */
    @Test
    void name() {
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            User user = new User((long) i, "张三" + i, i);
            list.add(user);
        }
        String json = JSONUtil.toJsonStr(list);
        System.out.println(json);
        List<User> list1 = JSONUtil.toList(json, User.class);
        log.info("list1 = {}", list1);
    }

    /**
     * json字符串转对象
     */
    @Test
    void name1() {
        ArrayList<DataDto> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            DataDto dto = new DataDto();
            dto.setKey("key" + i);
            dto.setValue("value" + i);
            list.add(dto);
        }
        String json = JSONUtil.toJsonStr(list);
        System.out.println(json);
        List<DataDto> list1 = JSONUtil.toList(json, DataDto.class);
        log.info("list1 = {}", list1);
    }

    /**
     * json字符串转对象
     */
    @Test
    void name2() {
        String json = "[{\"address\":\"重庆\",\"key\":\"张三\",\"value\":\"20\",\"desc\":\"\"},{\"address\":\"重庆\",\"key\":\"张三\",\"value\":\"20\",\"desc\":\"\"},{\"address\":\"重庆\",\"key\":\"张三\",\"value\":\"20\",\"desc\":\"\"}]";
        List<DataDto> list = JSONUtil.toList(json, DataDto.class);
        System.out.println(list);
    }

    /**
     * 属性带is
     */
    @Test
    void name3() {
        String json = "[{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"login_type\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"ldap-auto\",\"bodyParamDes\":\"\",\"id\":\"1\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"username\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"e-dengyan.tang\",\"bodyParamDes\":\"\",\"id\":\"2\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"password\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"ooS1Ys3LAHh5LDNBGGgn4t8EykSi9tQ/+bVLOKEO6/Psw1v4iHbpw3WVnedyyy7ZTiHX0axoSGQGwJcYMXuBPicxm2GrXdangmsYwim84lqJKBaQNEPnu8kk1br+lcBZBPm3lC+H6sugMboUNARhDF+VsIYHRE9b1KxoIu58Jbs=\",\"bodyParamDes\":\"\",\"id\":\"3\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"grant_type\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"password\",\"bodyParamDes\":\"\",\"id\":\"4\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"client_id\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"guc\",\"bodyParamDes\":\"\",\"id\":\"5\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"scope\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"openid\",\"bodyParamDes\":\"\",\"id\":\"6\",\"isbodyParamKey\":\"true\",\"fileList\":[]},{\"bodyParamType\":\"1\",\"isbodyParamDes\":\"true\",\"bodyParamKey\":\"captchaDisable\",\"isFileLoading\":\"false\",\"isbodyParamValue\":\"true\",\"bodyParamValueFile\":[],\"bodyParamValue\":\"true\",\"bodyParamDes\":\"\",\"id\":\"7\",\"isbodyParamKey\":\"true\",\"fileList\":[]}]";
        List<BodyParam> list = JSONUtil.toList(json, BodyParam.class);
        System.out.println(list);
    }

    /**
     * 属性带is
     */
    @Test
    void name30() {
        String json = """
                {
                	"bodyParamKey": "login_type",
                	"isBodyParamKey": "true"
                }
                """;
        BodyParam bodyParam = JSONUtil.toBean(json, BodyParam.class);
        System.out.println(bodyParam);
    }

    /**
     * 对象转字符串
     */
    @Test
    void name4() {
        BodyParam bodyParam = new BodyParam();
        bodyParam.setBodyParamKey("login_type");
        bodyParam.setBodyParamValue("ldap-auto");
        bodyParam.setBodyParamType(1);
        bodyParam.setBodyParamDes("");
        String json = JSONUtil.toJsonStr(bodyParam);
        System.out.println(json);
    }

    /**
     * 非标准化json字符串转json对象
     */
    @Test
    void name5() {
        String json = "{name:张三,age:10,status:\"1\",address:\"${address}\",num:$sys_random(10)}";
        JSON obj = JSONUtil.parse(json, PublicConstants.GLOBAL_JSON_CONFIG);
        System.out.println(JSONUtil.toJsonStr(obj));
    }

    /**
     * \@Transient测试
     */
    @Test
    void name6() {
        UserDto dto = new UserDto();
        dto.setName("张三");
        dto.setHeight(175.0);
        String json = JSONUtil.toJsonStr(dto, new JSONConfig().setTransientSupport(true));
        System.out.println(json);
    }

    /**
     * json对象转json字符串
     */
    @Test
    void name7() {
        JSON json = JSONUtil.parse(new User().setName("张三"));
        System.out.println(json.toString());
    }
}
